package me.chan.netty.basic;

import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class NettyFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        EventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();

        Future<String> result = eventLoop.submit(() -> {
            Thread.sleep(1000L);
            return "success";
        });

        log.info("waiting for result ...");
        // use future.get()
        // log.info("the result is : {}", result.get());

        // use genericChannelFuture.addListener()
        result.addListener((Future<String> f) -> {
            log.info("the result is {}", f.getNow());
        });
    }
}
